﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;

namespace QLTV_V2
{
    public partial class FrmBackUp : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmBackUp()
        {
            InitializeComponent();
        }
       
        private void FrmBackUp_Load(object sender, EventArgs e)
        {

        }
        ConnectData conn = new ConnectData();
        private void btn_SaoLuu_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Title = "Sao lưu CSDL...";
            saveFile.Filter = "BAK(*.bak)|*.bak";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                string sql = "backup database QLTV_V2 " +
                            "to disk='" + saveFile.FileName + "' " +
                            "with init";
                if (conn.ExecuteQuery(sql) == false)
                    MessageBox.Show("Thành công", "Thông báo");
                else
                    MessageBox.Show("Lỗi", "Thông báo");
            }
        }

        private void btn_PhucHoi_Click(object sender, EventArgs e)
        {
            if (txt_Link.Text == "")
                MessageBox.Show("Chưa chọn đường dẫn");
            else
                if (txt_TenFile.Text == "")
                    MessageBox.Show("Chưa đặt tên");
                else
                { 
                string sql="use master "+
                            "alter database QLTV_V2 "+
                            "set single_user with rollback immediate "+
                            "restore database QLTV_V2 "+
                            "from disk='" + txt_Link.Text + "' with replace " +
                            "alter database QLTV_V2 "+
                            "set multi_user with rollback immediate";
                if (conn.ExecuteQuery(sql) == false)
                    MessageBox.Show("Thành công", "Thông báo");
                else
                    MessageBox.Show("Lỗi", "Thông báo");
                }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Brown_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Multiselect = false;
            openFile.Title = "Phục hồi CSDL";
            openFile.Filter = "BAK(*.bak)|*.bak";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                txt_Link.Text = openFile.FileName;
                txt_TenFile.Text = openFile.FileName.Split('\\').Last();
            }
        }

    }
}
